package com.wxdc.nmgyt.nmgytwydc.presenter;

import com.wxdc.nmgyt.nmgytwydc.base.RxPersenter;
import com.wxdc.nmgyt.nmgytwydc.http.RetrofitHelper;
import com.wxdc.nmgyt.nmgytwydc.model.CbYfInfoBean;
import com.wxdc.nmgyt.nmgytwydc.model.GmYfInfoBean;
import com.wxdc.nmgyt.nmgytwydc.model.PicUploadStateBean;
import com.wxdc.nmgyt.nmgytwydc.model.YdInfoBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.YdAddresultBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.YfEditResultBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.YfRemoveReusltBean;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.BaseWebX5Contract;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/8/31.
 */

public class BaseWebX5Persenter extends RxPersenter<BaseWebX5Contract.View> implements BaseWebX5Contract.Persenter {
    private RetrofitHelper mRetrofitHelper;
    @Inject
    public BaseWebX5Persenter(RetrofitHelper retrofitHelper) {
        mRetrofitHelper = retrofitHelper;
    }

    /**
     * 单图片上传
     * @param picurl
     */
    @Override
    public void singleUploadPic(String picurl) {
        File imgFile = new File(picurl);
        // create RequestBody instance from file
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/png"), imgFile);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("uploaded_file_pic", imgFile.getName(), requestFile);

        mRetrofitHelper.singleUpLoadPic(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PicUploadStateBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(PicUploadStateBean results) {
                        mView.singlePicUploadResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error11");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 多图片上传
     * @param urls
     */
    @Override
    public void multiUploadPics(List<String> urls) {
        Map<String, RequestBody> maps = new HashMap<>();
        for (String url:urls) {
            File file = new File(url);
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            // 一定要加("AttachmentKey\"; filename=\"" +，不然失败
            maps.put("AttachmentKey\"; filename=\"" +file.getName(), requestBody);
        }
        mRetrofitHelper.multiUpLoadPics(maps)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PicUploadStateBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(List<PicUploadStateBean> picUploadStateBeans) {
                        mView.multiPicUploadResult(picUploadStateBeans);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void findYdInfo(String sampleplotid) {
        mRetrofitHelper.findYdInfoByPk(sampleplotid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(ResponseBody results) {
                        mView.isYdInfoExist(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error"+sampleplotid);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void editYdInfo(YdInfoBean obj) {
        mRetrofitHelper.editYdInfo(obj)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<YdAddresultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(YdAddresultBean results) {

                        mView.editYdInfoResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void addYdInfo(YdInfoBean obj) {
        mRetrofitHelper.addYdInfo(obj)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<YdAddresultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(YdAddresultBean results) {

                        mView.addYdInfoResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //---------------------------------------------草本-----------------------------------------------
    @Override
    public void findCbyfInfos(String ydbh) {
        mRetrofitHelper.queryCbyfInfo(ydbh)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CbYfInfoBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(List<CbYfInfoBean> results) {
                        mView.findCbyfInfoResults(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    @Override
    public void addCbyfInfo(CbYfInfoBean obj) {
        mRetrofitHelper.addCbyfInfo(obj)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<YfEditResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(YfEditResultBean results) {
                        mView.addCbyfInfoResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void editCbyfInfo(CbYfInfoBean obj) {
        mRetrofitHelper.editCbyfInfo(obj)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<YfEditResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(YfEditResultBean results) {
                        mView.editCbyfInfoResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void removeCbyfInfo(String grassid) {
        mRetrofitHelper.removeCbyfInfo(grassid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<YfRemoveReusltBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(YfRemoveReusltBean results) {
                        mView.removeCbyfInfoResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //--------------------------灌木-----------------------------------------------------------------
    @Override
    public void findGmyfInfos(String ydbh) {
        mRetrofitHelper.queryGmyfInfo(ydbh)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<GmYfInfoBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(List<GmYfInfoBean> results) {
                        mView.findGmyfInfoResults(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void addGmyfInfo(GmYfInfoBean obj) {
        mRetrofitHelper.addGmyfInfo(obj)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<YfEditResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(YfEditResultBean results) {
                        mView.addGmyfInfoResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void editGmyfInfo(GmYfInfoBean obj) {
        mRetrofitHelper.editGmyfInfo(obj)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<YfEditResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(YfEditResultBean results) {
                        mView.editGmyfInfoResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void removeGmyfInfo(String shrubid) {
        mRetrofitHelper.removeGmyfInfo(shrubid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<YfRemoveReusltBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(YfRemoveReusltBean results) {
                        mView.removeGmyfInfoResult(results);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
